<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body onload="verifyToken(),initRole()">
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">项目类型</label>
                            <div class="layui-input-inline" lay-filter="classDiv">
                                <select id="projectType" name="projectType" lay-filter="classIdFilter">
                                    <option selected>请选择</option>
                                    <option>内贸流通发展专项资金项目</option>
                                    <option>外经贸发展专项资金项目</option>
                                    <option>服务外包业产业发展专项资金项目</option>
                                    <option>区域服务业发展专项资金项目</option>
                                    <option>市商务委部门专项资金项目</option>
                                    <option>规划分配专项资金项目</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">项目名称</label>
                            <div class="layui-input-inline">
                                <input type="text" id="name" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">启动时间</label>
                            <div class="layui-input-inline">
                                <input type="text" id="startTime" name="startTime"  placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">结束时间</label>
                            <div class="layui-input-inline">
                                <input type="text" id="endTime" name="endTime"  placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">所属部门</label>
                            <div class="layui-input-inline">
                                <select id="department" name="department" lay-filter="classIdFilter">
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">备案状态</label>
                            <div class="layui-input-inline">
                                <select id="putOnRecords" name="putOnRecords" lay-filter="classIdFilter">
                                    <option selected>请选择</option>
                                    <option>未申请</option>
                                    <option>未备案</option>
                                    <option>已备案</option>
                                    <option>未通过</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">验收状态</label>
                            <div class="layui-input-inline">
                                <select id="checkAccept" name="checkAccept" lay-filter="classIdFilter">
                                    <option selected>请选择</option>
                                    <option>未验收</option>
                                    <option>已验收</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <!--<button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>-->
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter">

        </table>

        <script type="text/html" id="currentTableBar">
            {{# if(d.putOnRecords == 0 || d.putOnRecords == null){ }}
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-applyPutOnRecords"
               lay-event="applyPutOnRecords">备案</a>
            {{# }if(d.putOnRecords == 1){ }}
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-applyPutOnRecords"
               lay-event="">已备案</a>
            {{# }if(d.putOnRecords == -1){ }}
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-applyPutOnRecords"
               lay-event="">已退回</a>
            {{# }}}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="load">下载</a>
        </script>

    </div>
</div>
<script src="../js/lay-module/http/http.js"></script>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    // 初始化部门下拉表
    function initRole(){
        var $ = layui.jquery

        $.ajax({
            type: 'GET',
            url: $api + 'projectLibrary/department/select/all/department',
            contentType: "application/json;charset=utf-8",
            async: false,
            headers: {
                'Authorization': layui.data('table')['Authorization']
            },
            success: function (res) {
                // 对下拉列表进行拼接
                if (!res.code) {
                    html += "<option value=\"\" disabled selected>请选择</option>";
                    for (var i  = 0; i < res.data.length; i++) {
                        html += "<option value=\"" + res.data[i].id + "\">" + res.data[i].name + "</option>"
                    }
                     // console.log(html);
                    $("select[name='department']").html(html);
                    layui.form.render('select');
                }
            }
        });
    }

    layui.use(['form', 'table', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate;

        laydate.render({
            elem: '#startTime'
        });
        laydate.render({
            elem: '#endTime'
        });

        table.render({
            elem: '#currentTableId',
            url: $api + 'projectLibrary/project/select/project/by/type',
            toolbar: '#toolbarDemo',
            headers: {
                Authorization: layui.data('table')['Authorization']
            },
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                // {type: "checkbox", width: 50},
                {type: "numbers", width: 50},
                {field: "id", title: "主键", hide: true},
                {field: "project_id", title: "项目id", hide: true},
                {field: "name", title: "项目名称", width: 100},
                {field: "startTime", title: "启动时间", width: 100},
                {field: "endTime", title: "结束时间", width: 100},
                {field: "introduce", title: "简要介绍", width: 100},
                {field: "expectEffect", title: "预期效果", width: 100},
                {field: "projectType", title: "项目类型", width: 100, templet: function (res){
                    return getProjectType(res.projectType)
                    }
                },
                {field: "applicationTime", title: "申报时间", width: 100},
                {field: "firm", title: "所属单位", width: 100},
                {field: "department", title: "所属部门", width: 100},
                {field: "principal", title: "项目负责人", width: 100},
                {field: "qrCode", title: "二维码", hide: true},
                {field: "barCode", title: "条形码", hide: true},
                {field: "evolve", title: "项目进展", width: 100},
                {field: "putOnRecords", title: "是否备案", width: 100, templet: function (res) {
                        return getPutOnRecords(res.putOnRecords)
                    }
                },
                {field: "checkAccept", title: "是否验收", width: 100, templet: function (res) {
                        return getCheckAccept(res.checkAccept)
                    }
                },
                {field: "state", title: "项目状态", width: 100},
                {field: "achievement", title: "项目成果", width: 100},
                {title: '操作', minWidth: 250, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 5, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            done: function (res) {
                identifyCode(res)
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            console.log(result)
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    projectType: data.field.projectType,
                    name: data.field.name,
                    startTime: data.field.startTime,
                    endTime: data.field.endTime,
                    department: data.field.department,
                    putOnRecords: data.field.putOnRecords,
                    checkAccept: data.field.checkAccept
                },
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加项目',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/table/addProject.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log(JSON.stringify(data))
            if (obj.event === 'applyPutOnRecords') {

                $.ajax({
                    type: 'GET',
                    url: $api + 'projectLibrary/project/apply/putOnRecords?id=' + data.id,
                    contentType: "application/json;charset=utf-8",
                    async: false,
                    headers: {
                        'Authorization': layui.data('table')['Authorization']
                    },
                    success: function (res) {
                        alert(res.data)
                        window.location.reload($api + 'projectLibrary/project/select/project/by/type')
                    }
                });

                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {

                $.ajax({
                    type: 'GET',
                    url: $api + 'projectLibrary/project/delete/project?id=' + data.id,
                    contentType: "application/json;charset=utf-8",
                    async: false,
                    headers: {
                        'Authorization': layui.data('table')['Authorization']
                    },
                    success: function (res) {
                        alert(res.data)
                        window.location.reload($api + 'projectLibrary/project/select/project/by/type')
                    }
                });

            }else if(obj.event == 'load'){
                $.ajax({
                    type: 'GET',
                    url: $api + 'projectLibrary/project/create/pdf?id=' + data.id,
                    contentType: "application/json;charset=utf-8",
                    async: false,
                    headers: {
                        'Authorization': layui.data('table')['Authorization']
                    },
                    success: function (res) {
                        identifyCode(res)
                        window.location.href = res.data
                    }
                });
            }
        });

    });
</script>

</body>
</html>